$(function() {
  $('a.delete').click(function() {
    var that = $(this);
    var collection_id = $('div.collection').attr('data-collection-id');
    var product_id = that.attr('data-product-id');
    if (confirm('您确定要删除吗?')) {
      $.post('/collection/unpick', {'product_id':product_id, 'collection_id':collection_id}, function(data){
        if (data.code == '200') {
          that.parent().parent().fadeOut('slow'); 
        } else {
          alert('删除失败!');
        }
      }, 'json')
    }
  });
  $('.pick').click(function() {
    var that = $(this);
    var item = {
      'num_iid':'',
      'seller_id':'',
      'nick':'',
      'title':'',
      'price':'',
      'item_location':'',
      'seller_credit_score':'',
      'click_url':'',
      'shop_click_url':'',
      'pic_url':'',
      'taobaoke_cat_click_url':'',
      'keyword_click_url':'',
      'coupon_rate':'',
      'coupon_price':'',
      'coupon_start_time':'',
      'coupon_end_time':'',
      'commission_rate':'',
      'commission':'',
      'commission_num':'',
      'commission_volume':'',
      'volume':'',
      'shop_type':''
    };
    for (i in item) {
      item[i] = that.attr('data-' + i.replace('_', '-'));
    }
    $('#pick_img div.modal-body').empty();
    $('#pick_img div.modal-body').html('Loading ...');
    $.get('/collection/item_info/?r='+Math.random(), item, function(data) {
      var ul = $('<ul></ul>');
      for (i in data.imgs) {
        var li = $('<li style="list-style-type:none"></li>'); 
        var img = $('<img class="unselected" style="width:200px;height:200px;float:left;margin:15px;"/>');
        img.attr('src', data.imgs[i]['url']);
        if (data.imgs[i]['selected'] == 'y') img.addClass('selected').removeClass('unselected');
        li.append(img);
        ul.append(li);
      }
      if (ul.html() == '') {
        $('#pick_img div.modal-body').html('没有图片');
      } else {
        $('#pick_img div.modal-body').html(ul.html());
      }
      $('#pick_img div.modal-body li img').click(function() {
        if ($(this).hasClass('selected')) {
          $(this).removeClass('selected');
          $(this).addClass('unselected');
          selected.push($(this).attr('src'));
        } else {
          $(this).removeClass('unselected');
          $(this).addClass('selected');
          for (s in selected) {
            if (selected[s] == $(this).attr('src')) delete(selected[s]);
          }
        }
      });
    }, 'json');
    $('#pick_img').modal({
      keyboard: false
    })


    $('#select_all').click(function() {
      $('#pick_img img').addClass('selected').removeClass('unselected');
    });

    $('#unselect_all').click(function() {
      $('#pick_img img').addClass('unselected').removeClass('selected');
    });


    $('#pick_img a.save').unbind();
    $('#pick_img a.save').click(function() {
      var collection_id = $('div.collection').attr('data-collection-id');
      var img = [];
      $('#pick_img img.selected').each(function(k, v) {
        img.push($(this).attr('src'));
      });
  
      $.post('/collection/pick_taobao_save/', {'item':item, 'img':img, 'collection_id':collection_id}, function(data) {
        if (data.code == '200') {
          alert('操作成功!');
        } else {
          alert('操作失败!');
        }
      }, 'json');
    });
  });
});
